/*
 * Copyright (C) 2011 by Anton Novikau
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gplaces;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.Toast;

/**
 * Activity for application preference management.
 * 
 * @author Anton Novikov
 */
public class SettingsActivity extends PreferenceActivity {

    /**
     * Place type preference key used as search criterion in Google Places
     * search service.
     */
    public static final String PLACES_TYPE_PREF = "places_type";

    /**
     * Search radius preference key used as search criterion in Google Places
     * search service.
     */
    public static final String SEARCH_RADIUS_PREF = "search_radius";

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        // assign summary for palce type preference item.
        assignPlaceTypeSummary();

        // assign summary for search radius preference item and validate input
        // value.
        assignSearchRadiusSummary();
    }

    // method to assign summary for place type as current selected value.
    private void assignPlaceTypeSummary() {
        ListPreference placeType = (ListPreference) findPreference(PLACES_TYPE_PREF);
        CharSequence currentValue = placeType.getEntry();
        placeType.setSummary(currentValue);
        placeType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                ListPreference placeTypePref = (ListPreference) preference;
                placeTypePref.getEntryValues();
                int index = placeTypePref.findIndexOfValue(newValue.toString());
                placeTypePref.setSummary(placeTypePref.getEntries()[index]);
                return true;
            }
        });
    }

    // method to assign summary for search radius as current selected value.
    private void assignSearchRadiusSummary() {
        EditTextPreference searchRadius = (EditTextPreference) findPreference(SEARCH_RADIUS_PREF);
        searchRadius.setSummary(getString(R.string.search_radius_summary, searchRadius.getText()));
        final int maxRadius = getResources().getInteger(R.integer.max_search_radius);
        searchRadius.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                EditTextPreference searchRadiusPref = (EditTextPreference) preference;
                String newRadius = newValue.toString();
                try {
                    Integer radius = Integer.valueOf(newRadius);
                    if (radius <= 0 || radius > maxRadius) {
                        // notify user if entered number is bigger than max
                        // supported value.
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.search_radius_validate_msg, maxRadius),
                                Toast.LENGTH_LONG).show();
                        return false;
                    } else {
                        searchRadiusPref.setSummary(getString(R.string.search_radius_summary,
                                newRadius));
                        return true;
                    }
                } catch (NumberFormatException e) {
                    // number format could happens only if user entered too big
                    // number. Text enter is validated by field type.
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.search_radius_validate_msg, maxRadius),
                            Toast.LENGTH_LONG).show();
                    return false;
                }
            }
        });
    }
}
